package dev.ideas.funkyj.sequence;

import dev.ideas.funkyj.function.F;
import dev.ideas.funkyj.function._Function;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import static dev.ideas.funkyj.sequence.Sequence.*;

/**
 */
public class ToFork {

    @Test public void toFork() {
        Sequence<Boolean> sequence = from(8).to(bits);
        List<Boolean> booleans = sequence.asList();
        Assert.assertEquals(booleans, Arrays.asList(false, false, false, true));
        System.out.println("booleans = " + booleans);
    }

    _Function<Integer, Iterator<Boolean>> bits = new F<Integer, Iterator<Boolean>>() {
        @Override public Iterator<Boolean> apply(final Integer integer) {
            return new Iterator<Boolean>() {
                Integer val = integer;

                public boolean hasNext() { return val != 0; }
                public Boolean next() {
                    boolean result = (val % 2 == 1);
                    val /= 2;
                    return result;
                }
                public void remove() { throw new UnsupportedOperationException(); }
            };
        }
    };

}
